var app = new Vue({
    el: '.box',
    data: {
        open: false,
        nav: '',
        panel: '',
        tag: '',
        new_items: '',
        hot_items: '',
        expands: true,
        links: '',
        datas: [],
        ad_data: '',
        book_id: '',
        like_arr: [],
        openAlert: false
    },
    methods: {
        toggle(panel) {
            this.panel = panel === this.panel ? '' : panel
        }, set_tips(obj, flag) {
            if (flag) {
                return '更新时间: ' + obj
            } else {
                return '已有 ' + obj + ' 人看过'
            }
        }, like_click() {
            var id = this.book_id;
            $('.like').classList.add('like_clicks');
            this.like_arr = localStorage.getItem('like') ? JSON.parse(localStorage.getItem('like')) : [];
            var len = this.like_arr.length || 0;
            for (var i = 0; i < len; i++) {
                if (id == this.like_arr[i]) {
                    return false
                }
            };
            this.like_arr.push(id);
            var arr = Array.from(new Set(this.like_arr));
            localStorage.setItem('like', JSON.stringify(arr));
            get_info(109, id)
        }, gift_click() {
            this.openAlert = true
        }, openAlertDialog() {
            this.openAlert = true
        }, closeAlertDialog() {
            this.openAlert = false
        }
    },
    mounted() {
        var id = window.location.href.split('item/')[1].split('.html')[0];
        this.book_id = id;
        var arr = localStorage.getItem('like') ? JSON.parse(localStorage.getItem('like')) : [];
        var len = arr.length || 0;
        for (var i = 0; i < len; i++) {
            if (id == arr[i]) {
                $('.like').classList.add('like_clicks')
            }
        }
    }
});

function get_info(id, ids) {
    axios({
        method: "POST",
        baseURL: '../../api/index.php',
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify({
            head: id,
            ids: ids
        })
    }).then(function (res) {
        if (res.data.code === '200') {
            set_vue(res.data.data, id)
        } else {
            console.log(res.data.msg)
        }
    }).
        catch(function (err) {
            console.log(err)
        })
};

function set_vue(obj, id) {
    var len = obj.length;
    if (id === 108) {
        for (var i = 0; i < len; i++) {
            if (obj[i].ad_img) {
                obj[i].ad_img = "../../" + obj[i].ad_img.split('../../../')[1]
            }
        };
        app.ad_data = obj
    } else {
        for (var i = 0; i < len; i++) {
            obj[i].href = './' + obj[i].id + '.html'
        }
    };
    if (id === 100) {
        app.nav = obj
    };
    if (id === 102) {
        app.tag = obj
    };
    if (id === 103) {
        app.new_items = obj
    };
    if (id === 104) {
        app.hot_items = obj
    };
    if (id === 105) {
        app.links = obj
    }
};
get_info(100);
get_info(102);
get_info(103);
get_info(104);
get_info(105);
get_info(108);
window.onresize = function () {
    var w = document.documentElement.clientWidth || document.body.clientWidth;
    if (w <= 830) {
        app.expands = false
    } else {
        app.expands = true
    }
};
var w = document.documentElement.clientWidth || document.body.clientWidth;
if (w <= 830) {
    app.expands = false
} else {
    app.expands = true
};
hljs.initHighlightingOnLoad();
window.onload = function () {
    var id = window.location.href.split('item/')[1].split('.html')[0];
    app.book_id = id;
    get_info(110, id);
    var flag = false;
    if (flag) {
        axios({
            method: "POST",
            baseURL: '../../api/index.php',
            headers: {
                "Content-type": "application/json; charset=utf-8"
            },
            data: JSON.stringify({
                head: 200,
                id: id
            })
        }).then(function (res) {
            if (res.data.code === '200') {
                if (res.data.pre[0]) {
                    if (res.data.pre[0].flg - 0) {
                        res.data.pre[0].href = './' + res.data.pre[0].Id + '.html';
                        app.datas.push(res.data.pre[0])
                    } else {
                        res.data.pre[0] = {
                            title: '--',
                            href: 'javascript:;'
                        };
                        app.datas.push(res.data.pre[0])
                    }
                } else {
                    res.data.pre[0] = {
                        title: '没有了~',
                        href: 'javascript:;'
                    };
                    app.datas.push(res.data.pre[0])
                };
                if (res.data.next[0]) {
                    if (res.data.next[0].flg - 0) {
                        res.data.next[0].href = './' + res.data.next[0].Id + '.html';
                        app.datas.push(res.data.next[0])
                    } else {
                        res.data.next[0] = {
                            title: res.data.next[0].title,
                            href: 'javascript:;'
                        };
                        app.datas.push(res.data.next[0])
                    }
                } else {
                    res.data.next[0] = {
                        title: '--',
                        href: 'javascript:;'
                    };
                    app.datas.push(res.data.next[0])
                }
            } else {
                console.log(res.data.msg)
            }
        }).
            catch(function (err) {
                console.log(err)
            })
    };
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js'
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
        var appid = 'cytGiGO6V';
        var conf = 'prod_6a01266dddc7d75b71e70f0d6c8fdd0d';
        var width = window.innerWidth || document.documentElement.clientWidth;
        if (width < 960) {
            window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>')
        } else {
            var loadJs = function (d, a) {
                var c = document.getElementsByTagName("head")[0] || document.head || document.documentElement;
                var b = document.createElement("script");
                b.setAttribute("type", "text/javascript");
                b.setAttribute("charset", "UTF-8");
                b.setAttribute("src", d);
                if (typeof a === "function") {
                    if (window.attachEvent) {
                        b.onreadystatechange = function () {
                            var e = b.readyState;
                            if (e === "loaded" || e === "complete") {
                                b.onreadystatechange = null;
                                a()
                            }
                        }
                    } else {
                        b.onload = a
                    }
                }
                c.appendChild(b)
            };
            loadJs("https://changyan.sohu.com/upload/changyan.js", function () {
                window.changyan.api.config({
                    appid: appid,
                    conf: conf
                })
            })
        }
    })();
};

function get_user_info() {
    var data = {
        href: window.location.href,
        title: document.title,
        timer: 5000,
        source: document.referrer
    };
    axios({
        method: "POST",
        baseURL: '../../api/log.php',
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(data)
    }).then(function (res) { }).
        catch(function (err) {
            console.log(err)
        })
};
setTimeout(function () {
    get_user_info()
}, 5000);